//
//  MineItemToolView.m
//  ShowYork
//
//  Created by hxh on 2021/3/5.
//  Copyright © 2021 hxh. All rights reserved.
//

#define ITEMHEIGHT 50.0

typedef void(^doneBlock)(NSInteger index);
#import "MineItemToolView.h"

@interface MineItemToolView ()
@property (nonatomic,copy)NSArray<NSString *>*tilteArray;
@property (nonatomic,copy)NSArray<NSString *>*iconArray;
@property (nonatomic,copy)doneBlock done;
@property (nonatomic,strong)UIView *shadowView;
@property (nonatomic,strong)UIView *containView;

@end

@implementation MineItemToolView

- (instancetype)initWithItemTitles:(NSArray<NSString *> *)titles itemIcons:(NSArray<NSString *> *)icons completeBlock:(nonnull void (^)(NSInteger index))completeBlock{
    if (self = [super init]) {
        self.tilteArray = titles;
        self.iconArray = icons;
        [self setUI];
        if (completeBlock) {
            self.done = ^(NSInteger index) {
                completeBlock(index);
            };
        }
    }
    return self;
}

- (void)setUI{
    

    
    
    self.shadowView             = [UIView new];
    [self addSubview:self.shadowView];
    
    self.shadowView.layer.shadowColor   = RGBA(240, 240, 240, 1).CGColor;
    self.shadowView.layer.shadowOffset  = CGSizeMake(0, 0);
    self.shadowView.layer.shadowOpacity = 0.8;
    self.shadowView.layer.shadowRadius  = 4;
    
    self.containView = [[UIView alloc]init];
    self.containView.layer.backgroundColor = [UIColor whiteColor].CGColor;
    self.containView.layer.shadowColor = RGBA(240, 240, 240, 1).CGColor;
    self.containView.layer.shadowOffset = CGSizeMake(0,0);
    self.containView.layer.shadowOpacity = 1;
    self.containView.layer.shadowRadius = 5;
    self.containView.layer.cornerRadius = 8;
    self.containView.layer.masksToBounds = YES;
    [self.shadowView addSubview:self.containView];
    
    
    
    [self.shadowView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.top.bottom.equalTo(self.containView);
    }];
    
    [self.containView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.right.bottom.equalTo(self);
    }];
    
    [self.tilteArray enumerateObjectsUsingBlock:^(NSString * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        if (idx < self.iconArray.count) {
            NSString *iconName =  self.iconArray[idx];
            UIButton *btn = [[UIButton alloc]init];
            btn.tag = idx;
            
            [btn addTarget:self action:@selector(buttonTouchDown:) forControlEvents:UIControlEventTouchDown];
        
            [btn addTarget:self action:@selector(buttonClick:) forControlEvents:UIControlEventTouchUpInside];
            
            [btn addTarget:self action:@selector(buttonCancel:) forControlEvents:UIControlEventTouchUpOutside];
            
            [self.containView addSubview:btn];
            
            UILabel *lb = [[UILabel alloc]init];
            lb.text = obj;
            lb.textColor = C51;
            lb.font = font(14);
            [self.containView addSubview:lb];
            UIImageView *imgV = [[UIImageView alloc]initWithImage:[UIImage imageNamed:iconName]];
            [self.containView addSubview:imgV];
            
            UIImageView *nextIcon = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"mine_access_tag"]];
            [self.containView addSubview:nextIcon];
            
            if (idx == self.tilteArray.count - 1) {
                [btn mas_makeConstraints:^(MASConstraintMaker *make) {
                    make.top.equalTo(self).offset(idx * ITEMHEIGHT);
                    make.left.right.equalTo(self);
                    make.height.mas_equalTo(ITEMHEIGHT);
                    make.bottom.equalTo(self);
                }];
            }else{
                [btn mas_makeConstraints:^(MASConstraintMaker *make) {
                    make.top.equalTo(self).offset(idx * ITEMHEIGHT);
                    make.left.right.equalTo(self);
                    make.height.mas_equalTo(ITEMHEIGHT);
                }];
            }
            [imgV mas_makeConstraints:^(MASConstraintMaker *make) {
                make.centerY.equalTo(btn);
                make.left.equalTo(self).offset(16);
            }];
            [lb mas_makeConstraints:^(MASConstraintMaker *make) {
                make.centerY.equalTo(btn);
                make.left.equalTo(imgV.mas_right).offset(16);
            }];
            [nextIcon mas_makeConstraints:^(MASConstraintMaker *make) {
                make.right.equalTo(self).offset(-16);
                make.centerY.equalTo(btn);
            }];
        }
    }];
}

- (void)buttonTouchDown:(UIButton *)btn{
    btn.backgroundColor = RGBA(240, 240, 240, 1);
}

- (void)buttonClick:(UIButton *)btn{
    self.done(btn.tag);    
    btn.backgroundColor = [UIColor whiteColor];
}
- (void)buttonCancel:(UIButton *)btn{
    btn.backgroundColor = [UIColor whiteColor];
}

@end
